<?php
class Model  {

    //数据库对象
    var $db = null;

    var $db_prefix = '';

    var $table = null;

    /* 主键 */
    var $prikey = null;

    /* 别名 */
    var $alias = null;

    /* 模型的名称 */
    var $_name   = null;

    public function __construct()
    {
        $this->BaseModel();

    }
    /**
     *  构造函数
     *
     *  @author Garbin
     *  @param  array  $params
     *  @param  object $db
     *  @return void
     */
    function BaseModel()
    {
        static $db = null;
        if ($db === null) {
            $config = include_once(ROOTPATH . '/config/db.config.php');
            include_once(ROOTPATH . '/core/db/mysql.class.php');
            $db = new db_mysqli($config);
        }
        $this->db = $db;
        $this->db_prefix = $config['db_prefix'];
    }
    function get($param=array()){
        if(is_numeric($param) && $param>0){
            $where = ' WHERE '. $this->prikey.'='.$param;
        }else{
            $where = !empty($param['conditions']) ? ' where '.$param['conditions'] : '';
        }
        $fields = !empty($param['fields']) ? $param['fields'] : '*';
        $sql = 'select '.$fields.' from '.$this->db_prefix.$this->table.$where;
        return $this->db->get_row($sql);
    }

    function find($param=array()){
        $fields = !empty($param['fields']) ? $param['fields'] : '*';
        $where = !empty($param['conditions']) ? ' WHERE '.$param['conditions'] : '';
        $sql = 'select '.$fields.' from '.$this->db_prefix.$this->table.$where;
        return $this->db->get_all($sql);

    }

    function edit($conditions,$param=array()){
        if(is_numeric($conditions) && $conditions>0){
            $where = ' WHERE '. $this->prikey.'='.$conditions;
        }else{
            $where = !empty($conditions) ? ' WHERE '.$conditions : '';
        }

        $sql = $this->db->get_update_db_sql($this->db_prefix.$this->table,$param,$where);
        return $this->db->query($sql);
    }

    function drop($conditions){
        if(is_numeric($conditions) && $conditions>0){
            $where = ' WHERE '. $this->prikey.'='.$conditions;
        }else{
            $where = !empty($conditions) ? ' WHERE '.$conditions : '';
        }
        $sql = 'delete from '.$this->db_prefix.$this->table.$where;
        return $this->db->query($sql);
    }

    function add($info){
        $sql = $this->db->get_insert_db_sql($this->db_prefix.$this->table,$info);
        return $this->db->query($sql);
    }

}


?>